let prisma = require("../util/prismaBase")
let returnUtil = require("../util/returnUtil")




exports.allReports = async (req, res) => {
    console.log("查询所有", req.body.params)
    try {
        let result = await prisma.tb_workReport.findMany({
            where: req.body.params,
            include: {
                Member: {
                    include: {
                        depart: true
                    }
                },
                Machine: true
            },
            orderBy: {
                create_time: 'desc'
            }
        })
        res.send(returnUtil.getReturn(result))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }
 
}
exports.listGroupReport = async (req, res) => {
    console.log("获取列表", req.body.params)
    if (typeof (req.body.pageIndex) == 'undefined') {
        req.body.pageIndex = 0
    }
    if (typeof (req.body.pageSize) == 'undefined') {
        req.body.pageSize = 10
    }
    try {
        let result = await prisma.tb_workReport.groupBy({
            by: ['member_id', 'work_name', 'xinghao'],
            _sum: {
                work_num: true,
            },
        })
        res.send(returnUtil.getReturn(result))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }
}


exports.listReport = async (req, res) => {
    console.log("获取列表", req.body.params)
    if (typeof (req.body.pageIndex) == 'undefined') {
        req.body.pageIndex = 0
    }
    if (typeof (req.body.pageSize) == 'undefined') {
        req.body.pageSize = 10
    }
    try {
        let result = await prisma.tb_workReport.findMany({
            where: req.body.params,
            skip: req.body.pageIndex,
            take: req.body.pageSize,
            include: {
                Member: {
                    include: {
                        depart: true
                    }
                },
                Machine: true
            },
            orderBy: {
                create_time: 'desc'
            }
        })
        let count = await prisma.tb_workReport.count({
            where: req.body.params,
        })
        res.send(returnUtil.getReturn(result, { count: count }))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }
}

exports.batchAdd = async (req, res) => {
    let params = req.body.params
    console.log("批量添加", params)
    try {
        let batchArr = []
        for (var pam of params) {
            let addParam = {
                member_id: pam.tx_name,
                xinghao: pam.xinghao_name,
                machine_id: pam.tx_machine,
                night_class: Number(pam.night_class),
                work_count: Number(pam.tx_work_count),
                work_name: pam.work_name,
                work_num: Number(pam.tx_work_number),
                work_times: Number(pam.tx_work_time),
                standard_time: Number(pam.standard_time),
                standard_num: Number(pam.standard_num),
                work_date: Number(pam.work_date),
                ng_num:Number(pam.tx_ng_number)
            }
            let addThing = prisma.tb_workReport.create({
                data: addParam
            })
            batchArr.push(addThing)
        }
        let result = await prisma.$transaction(batchArr)
        res.send(returnUtil.getReturn(result))
    } catch (error) {
        console.log(error);
        res.send(returnUtil.getError(JSON.stringify(error)))
    }
}

exports.addReport = async (req, res) => {
    let pam = req.body.params;
    console.log("添加日报", pam)
    let addParam = {
        member_id: pam.tx_name,
        xinghao_id: pam.tx_work_xinghao_name,
        work_count: Number(pam.tx_work_count),
        work_name: pam.work_name,
        work_num: Number(pam.tx_work_number),
        work_times: Number(pam.tx_work_time),
        standard_time: Number(pam.standard_time),
        standard_num: Number(pam.standard_num),
        work_date: Number(pam.work_date)
    }
    try {
        let result = await prisma.tb_workReport.create({
            data: addParam
        })
        res.send(returnUtil.getReturn(result))

    } catch (error) {
        console.log("添加日报异常error:", error)
        res.send(returnUtil.getError("添加日报异常"))
    }

}

exports.delReport = async (req, res) => {
    let id = req.body.id;
    console.log("删除日报", req.body.id)
    try {
        let result = await prisma.tb_workReport.delete({
            where: {
                id: req.body.id
            }
        })
        res.send(returnUtil.getReturn(result))

    } catch (error) {
        console.log("删除日报异常error:", error)
        res.send(returnUtil.getError("删除日报异常"))
    }

}

exports.editReport = async (req, res) => {
    let params = req.body.params;
    let id = req.body.id
    console.log("编辑日报", params, "id:", id)
    try {
        let result = await prisma.tb_workReport.update({
            data: params,
            where: {
                id: id
            }
        })
        res.send(returnUtil.getReturn(result))

    } catch (error) {
        console.log("编辑日报异常error:", error)
        res.send(returnUtil.getError("编辑日报异常"))
    }

}